import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:oktoast/oktoast.dart';
import 'package:yurun_smartmerchants/net/ApiInterface.dart';
import 'package:yurun_smartmerchants/net/NetUtil.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/routes/routes.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';
import 'package:yurun_smartmerchants/utils/evenbus_utils.dart';
import 'package:yurun_smartmerchants/utils/loading_utils.dart';
import 'package:yurun_smartmerchants/models/custom_list_entity.dart';
import 'package:yurun_smartmerchants/view/EmptyView.dart';

class CustomAdministration extends StatefulWidget {
  @override
  _CustomAdministrationState createState() => _CustomAdministrationState();
}

class _CustomAdministrationState extends State<CustomAdministration> {
  var bus = EventBus();
  List<CustomListContent> listItem;
  final int pageSize = 10;
  int pageNum = 0;
  EasyRefreshController controller = EasyRefreshController();
  bool isIntoPageRefresh = true; //判断是不是第一次进入页面的刷新

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      getList();
    });
    bus.on("addCustom", (arg) {
      isIntoPageRefresh = true;
      pageNum = 0;
      getList();
    });
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
    bus.off("addCustom");
  }

  void getList() {
    if (isIntoPageRefresh) {
      LoadingUtil.showLoading(context, tip: '加载中...');
    }
    ApiInterface.getCustomList(pageSize, pageNum).then((value) {
      CustomListEntity customListEntity = CustomListEntity.fromJson(value);
      setState(() {
        if (pageNum == 0) {
          listItem = customListEntity.content;
        } else {
          listItem.addAll(customListEntity.content);
        }
      });
      overLoad(true, customListEntity.last);
      if (listItem != null && listItem.length > 0) {
        if (!isIntoPageRefresh && pageNum == 0) {
          showToast("刷新成功");
        } else if (customListEntity.last && pageNum > 0) {
          showToast("暂无更多数据");
        }
      }
    }).catchError((error) {
      overLoad(false, true);
    }).whenComplete(() => {
          if (isIntoPageRefresh) {LoadingUtil.hideLoading(context)}
        });
  }

  void overLoad(bool isSuccess, bool islast) {
    if (pageNum == 0) {
      controller.finishRefresh(success: isSuccess);
      controller.finishLoad(success: isSuccess, noMore: islast);
    } else {
      controller.finishLoad(success: isSuccess, noMore: islast);
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colours.white,
        centerTitle: true,
        iconTheme: IconThemeData(color: Colours.app_words),
        title: Text(
          '客户管理',
          style: TextStyle(fontSize: 18, color: Colours.app_words),
        ),
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        color: Colours.gray_ef,
        child: Column(
          children: <Widget>[
            Expanded(
              child: Container(
                  width: double.infinity,
                  height: double.infinity,
                  margin: EdgeInsets.only(
                      top: AppSize.sp(20), bottom: AppSize.sp(20)),
                  child: Stack(children: <Widget>[
                    EasyRefresh(
                        controller: controller,
                        header: MaterialHeader(),
                        footer: MaterialFooter(),
                        onRefresh: () async {
                          isIntoPageRefresh = false;
                          pageNum = 0;
                          getList();
                        },
                        onLoad: () async {
                          isIntoPageRefresh = false;
                          pageNum++;
                          getList();
                        },
                        child: ListView.builder(
                            itemBuilder: (context, index) {
                              return itemWidget(context, index);
                            }, itemCount: listItem == null ? 0 : listItem.length)),
                   // EmptyView.show(listItem != null && listItem.length == 0 ? false : true, null, "暂无客户"),
                    EmptyView(
                      isHide:listItem != null && listItem.length == 0 ? false : true,
                      content:"暂无客户" ,
                    )
                  ])),
            ),
            GestureDetector(
                onTap: () {
                  Navigator.pushNamed(context, custom_add);
                },
                child: Container(
                    height: AppSize.height(105),
                    color: Colours.white,
                    child: Container(
                        margin: EdgeInsets.all(AppSize.sp(12)),
                        height: AppSize.height(82),
                        alignment: Alignment.center,
                        decoration: new BoxDecoration(
                          color: Colours.app_main,
                          borderRadius: BorderRadius.all(
                            Radius.circular(AppSize.sp(5)),
                          ),
                        ),
                        child: Row(
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: Container(
                                  alignment: Alignment.centerRight,
                                  margin:
                                      EdgeInsets.only(right: AppSize.sp(20)),
                                  child: Image.asset(
                                    'images/approach_add.png',
                                    width: AppSize.width(43),
                                    height: AppSize.height(43),
                                  )),
                            ),
                            Expanded(
                                flex: 1,
                                child: Container(
                                    alignment: Alignment.centerLeft,
                                    child: Text(
                                      "新增客户",
                                      style: TextStyle(
                                          color: Colours.white,
                                          fontSize: AppSize.sp(32)),
                                    )))
                          ],
                        ))))
          ],
        ),
      ),
    );
  }

  Widget itemWidget(BuildContext context, int position) {
    return GestureDetector(
      onTap: () {
        Navigator.pushNamed(context, custom_add, arguments: listItem[position]);
      },
      child: Container(
        color: Colours.white,
        width: double.infinity,
        height: AppSize.height(128),
        margin: EdgeInsets.only(bottom: AppSize.sp(1)),
        child: Row(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left: AppSize.sp(32)),
              child: CircleAvatar(
                backgroundColor: Colours.color_adb0ba,
                child: Container(
                  child: Icon(
                    Icons.people,
                    color: Colours.white,
                    size: AppSize.sp(70),
                  ),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: AppSize.sp(28)),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    child: Text(listItem[position].name,
                        style: TextStyle(
                            fontSize: AppSize.sp(32),
                            color: Colours.color_434343)),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: AppSize.sp(17)),
                    child: Text(listItem[position].phone,
                        style: TextStyle(
                            fontSize: AppSize.sp(26),
                            color: Colours.color_898989)),
                  ),
                ],
              ),
            ),
            Spacer(),
            Container(
              margin: EdgeInsets.only(right: AppSize.sp(32)),
              child: Icon(
                Icons.arrow_forward_ios,
                color: Colours.color_c7c7c7,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
